package khs.java_time.main.java;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 * Hello world!
 *
 */
public class App 
{
    private static BufferedReader bufferedReader;
    
    public static void main( String[] args ) throws IOException, NumberFormatException, InterruptedException
    {
        bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String input_mode;
        
        do{
            fnPrintMode();
            input_mode = bufferedReader.readLine();
            
            switch(input_mode){
            case "1":
                fnLastdayMode();
                break;
            case "2":
                fnSleepMode();
                break;
            case "3":
                fnYoonYearMode();
                break;
            case "4":
                fnTime2StringMode();
                break;
            }
        }while(!input_mode.equals("q"));
        
        bufferedReader.close();
    }
    
    public static void fnPrintMode(){
        System.out.println("1. last day");
        System.out.println("2. sleep(milli)");
        System.out.println("3. yoon year");
        System.out.println("4. time2string");
        System.out.println("q. quit");
    }
    
    public static void fnLastdayMode() throws IOException
    {
        System.out.print("input year: ");
        String input_year = bufferedReader.readLine();
        
        System.out.print("input month: ");
        String input_month = bufferedReader.readLine();
        
        int lastday = fnLastday(Integer.valueOf(input_year), Integer.valueOf(input_month));
        System.out.println(lastday);
    }
    
    public static int fnLastday(int year, int month)
    {
        int rtn = 0;
        
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month-1);
        calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
        
        rtn = calendar.get(Calendar.DATE);
        
        return rtn;
    }
    
    public static void fnSleepMode() throws IOException, NumberFormatException, InterruptedException
    {
        System.out.print("input delay: ");
        String input_delay = bufferedReader.readLine();
        
        System.out.println(System.currentTimeMillis());
        
        fnSleep(Integer.valueOf(input_delay));
        
        System.out.println(System.currentTimeMillis());
    }
    
    public static void fnSleep(int delay) throws InterruptedException
    {
        Thread.sleep(delay);
    }
    
    public static void fnYoonYearMode() throws IOException
    {
        System.out.print("input year: ");
        String input_year = bufferedReader.readLine();
        
        boolean isYoonYear = fnYoonYear(Integer.valueOf(input_year));
        System.out.println(isYoonYear);
    }
    
    public static boolean fnYoonYear(int year)
    {
        GregorianCalendar gregorianCalendar = new GregorianCalendar();
        return gregorianCalendar.isLeapYear(year);
    }
    
    public static void fnTime2StringMode()
    {
        long currentTimeMillis = System.currentTimeMillis();
        String time2String = fnTime2String(currentTimeMillis);
        
        System.out.println(time2String);
    }
    
    public static String fnTime2String(long millis)
    {
        String rtn = "";
        
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(millis);
        
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        rtn = simpleDateFormat.format(calendar.getTime());
        
        return rtn;
    }
}
